package com.java.netty.client.vnc;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.nio.charset.StandardCharsets;

import com.alibaba.fastjson.JSONObject;
import com.java.netty.common.codec.VncDecoder;
import com.java.netty.common.codec.VncEncoder;
import com.java.netty.common.codec.VncPackageContent;
import com.java.netty.common.codec.VncPacket;
import com.java.netty.common.helper.VncHelper;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;

public class StartClient {

   final static String host = "127.0.0.1";
   final static int port = 40000;

   public static void main(String[] args) throws Exception {
      Bootstrap bootstrap = new Bootstrap();
      NioEventLoopGroup group = new NioEventLoopGroup();
      try {
         bootstrap.group(group).channel(NioSocketChannel.class).option(ChannelOption.SO_KEEPALIVE, true).handler(new ChannelInitializer<SocketChannel>() {
            @Override
            protected void initChannel(SocketChannel socketChannel) throws Exception {
               socketChannel.pipeline()
                  //如果去掉LengthFieldBasedFrameDecoder，LengthFieldPrepender 就能正确拿到消息版本，消息类型，和待命时间
                  //                                    .addLast(new LengthFieldBasedFrameDecoder(ByteOrder.LITTLE_ENDIAN, 1024, 0, 4, 0, 0, true))
                  //                                    .addLast(new LengthFieldPrepender(ByteOrder.LITTLE_ENDIAN, 4, 0, false))
                  .addLast(new VncDecoder()).addLast(new VncEncoder()).addLast(new LoggingHandler(LogLevel.INFO)).addLast(new ClientHandler());
            }
         });
         VncPackageContent vncPackageContent = new VncPackageContent();
         vncPackageContent.setTarget(0);
         vncPackageContent.setSource(0);
         vncPackageContent.setVersion((byte) 1);
         vncPackageContent.setTag(0);
         vncPackageContent.setCommand(0);
         vncPackageContent.setParamType((byte) 0x0000);

         VncPacket vncPacket = new VncPacket();
         vncPacket.setContent(vncPackageContent);
         vncPacket.setCrc32(1);
         vncPacket.setPkgSize(13);
         vncPacket.setDirty(false);
         byte[] msg = VncHelper.buildMsg(vncPacket);

         ChannelFuture cf = bootstrap.connect(host, port).sync();
         for (int i = 0; i < 1; i++) {
            System.out.println("第" + i + "次发送消息");
            System.out.println(vncPacket.toString());
            cf.channel().writeAndFlush(Unpooled.copiedBuffer(msg));
            BufferedWriter out = new BufferedWriter(new FileWriter("vncmsg.txt"));
            String s = new String(msg, StandardCharsets.UTF_8);
            out.write(s);
            out.close();
            System.out.println("文件创建成功！");
            System.out.println("----包长度为：" + msg.length);
         }
         System.out.println("往端口：" + port + "发送完成");
         cf.channel().closeFuture().sync();

      } finally {
         group.shutdownGracefully();
         System.out.println("客户端己释放");
      }
   }
}
